package org.anti996.lms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.anti996.lms.model.Truck;
import org.anti996.lms.service.TruckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 货车表 前端控制器
 * </p>
 *
 * @author The Anti996 Authors
 * @since 2022-07-15
 */
@Slf4j
@RestController
@RequestMapping("/lms/truck")
@CrossOrigin(origins = "*")
public class TruckController {
    @Autowired
    private TruckService truckService;
    /**
     * 获取货车列表
     *
     * @return 货车列表
     */
    @GetMapping
    public List<Truck> truckList() { return truckService.list();}
    /**
     * 获取货车分页列表
     *
     * @param no 第no个页面
     * @param sz 每个页面的数据条数
     * @return 分页列表
     */
    @GetMapping("/page/{sz}/{no}")
    public List<Truck> truckListPage(@PathVariable(name = "no") int no,
                                             @PathVariable(name = "sz") int sz) {
        Page<Truck> page = truckService.page(new Page<>(no, sz));
        return page.getRecords();
    }
    /**
     * 根据货车编号获取货车
     *
     * @param t_id 货车编号
     * @return 查询到的货车
     */
    @GetMapping("/{id}")
    public Truck truck(@PathVariable(name = "id") int t_id) {
        return truckService.getById(t_id);
    }
    /**
     * 创建货车
     *
     * @param truck 货车信息
     * @return 创建结果
     */
    @PostMapping(consumes = "application/json")
    public boolean insertTruck(@RequestBody Truck truck) {
        return truckService.save(truck);
    }
    /**
     * 更新货车
     *
     * @param t_id  货车编号
     * @param truck 新的货车信息
     * @return 更新是否成功
     */
    @PutMapping(path = "/{id}", consumes = "application/json")
    public boolean updateTruck(@PathVariable(name = "id") int t_id,
                                   @RequestBody Truck truck) {
        return truckService.updateById(truck);
    }
    /**
     * 删除指定货车编号的数据
     *
     * @param t_id 货车编号
     * @return 是否删除成功
     */
    @DeleteMapping("/{id}")
    public boolean removeTruck(@PathVariable(name = "id") int t_id) {
        return truckService.removeById(t_id);
    }
}
